package com.fafu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class Register03 {
    public static void main(String[] args) throws Exception{

        Scanner in = new Scanner(System.in);
        System.out.println("请输入姓名");
        String acc = in.next();
        System.out.println("请输入密码");
        String pas = in.next();
        System.out.println("请输入性别");
        String sex = in.next();
        System.out.println("请输入");

        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=UTF-8","root","root");
        System.out.println(connection);
        //创建要执行的SQL语句
        String sql = "INSERT INTO `student` VALUES(DEFAULT,?,?,?,DEFAULT,DEFAULT,DEFAULT,DEFAULT)";
        //通过connection实例来创建PrepaaredStatement,将要执行的sql语句放入方法参数中
        PreparedStatement pstm = connection.prepareStatement(sql);
        //通过PreparedStatement对象为占位符赋值 psmt.setXxx(参数索引位置,具体值)
        pstm.setString(1,acc);
        pstm.setString(2,pas);
        pstm.setString(3,sex);
        //执行sql  insert update delete返回的都是受影响的行数excuteUpdate()  select返回的都是临时表excuteQuery()
        int i = pstm.executeUpdate();

        if(i>0){
            System.out.println("操作成功");
        }
        else{
            System.out.println("操作失败");
        }



    }
}
